package org.grayrabbit.cms.plugin.elements;

import java.util.Map;
import java.util.regex.Pattern;

import org.dom4j.Element;
import org.grayrabbit.cms.plugin.loaders.LoaderUtils;


/**
 * @author sys53
 * @version 1.0
 * @created 06-一月-2012 15:59:49
 */
public class ResourceDescriptor {
	private String type;
	private String name;
	private String location;
	private String contentType;
	private Pattern pattern;
	private String content;
	private Map<String,String> params;
	private ResourceLocation ourLocation;
	
	public ResourceDescriptor() {
		
	}

	public ResourceDescriptor(Element element) {
		this.type = element.attributeValue("type");
	    String name = element.attributeValue("name");
	    String namePattern = element.attributeValue("namePattern");
	    if ((name == null) && (namePattern == null))
	    {
	    	throw new RuntimeException("resource descriptor needs one of 'name' and 'namePattern' attributes.");
	    }
	    if ((name != null) && (namePattern != null))
	    {
	    	throw new RuntimeException("resource descriptor can have only one of 'name' and 'namePattern' attributes.");
	    }
	    this.name = name;
	    this.location = element.attributeValue("location");
	    if ((namePattern != null) && (!this.location.endsWith("/")))
	    {
	    	throw new RuntimeException("when 'namePattern' is specified, 'location' must be a directory (ending in '/')");
	    }
	    this.params = LoaderUtils.getParams(element);
	    if ((element.getTextTrim() != null) && (!"".equals(element.getTextTrim())))
	    {
	    	this.content = element.getTextTrim();
	    }
	    this.contentType = getParameter("content-type");
	    if (namePattern != null)
	    {
	    	this.pattern = Pattern.compile(namePattern);
	    }
	    else
	    {
	    	this.ourLocation = new ResourceLocation(this.location, name, this.type, this.contentType, this.content, this.params);
	    }
	}

	private String getParameter(String key) {
		return (String)this.params.get(key);
	}

	public String getName() {
		if (this.name == null)
	    {
	      throw new RuntimeException("tried to get name from ResourceDescriptor with null name and namePattern = " + this.pattern);
	    }
	    return this.name;
	}

	public String getType() {
		return this.type;
	}

	public boolean doesTypeAndNameMatch(String type, String name) {
		 if ((type != null) && (type.equalsIgnoreCase(this.type)))
		    {
		      if (this.pattern != null)
		      {
		        return this.pattern.matcher(name).matches();
		      }
		      return (name != null) && (name.equalsIgnoreCase(this.name));
		    }
		    return false;
	}

	public ResourceLocation getResourceLocationForName(String name) {
		 if (this.pattern != null)
		    {
		      if (this.pattern.matcher(name).matches())
		      {
		        return new ResourceLocation(getLocation(), name, this.type, this.contentType, this.content, this.params);
		      }
		      throw new RuntimeException("Thiss descriptor does not provide resources named " + name);
		    }
		    return this.ourLocation;
	}

	private String getLocation() {
		return this.location;
	}
}